Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-arraybuffer-reviver

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-arraybuffer-reviver

Utility functions for JSON.parse and JSON.stringify to work with typed arrays and array buffers.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

json-arraybuffer-reviver

Utility functions for JSON.parse and JSON.stringify to work with typed arrays and array buffers.

Installation

Install the library via NPM:

# npm install json-arraybuffer-reviver

Usage

Setup

Before you can use the functionality in your project, you need to import it to your project:

// Import the Price Oracle library.
import { replacer, reviver } from 'json-arraybuffer-reviver`;

Usage

When you need to JSON.stringify() or JSON.parse() data that contains ArrayBuffers, DataViews or BigInts, simply pass in the replacer and reviver functions:

// Create some data to encode.
const inputData = new Uint8Array();

// Encode the data into JSON.
const encodedData = JSON.stringify(inputData, replacer);

// Decode the data from JSON.
const outputData = JSON.parse(encodedData, reviver);

Details

The replacer function looks at the data that you are trying to encode into JSON and instead of using the default toString() methods it encodes the data in a human readable form as follows:

BigIntegers:
{
	$BigInt: 'The integer number as a string'
}
ArrayBuffers:
{
	$ArrayBuffer:
	{
		View: "Name of the ArrayBuffer or DataView type, as a string",
		Data: "Hexadecimal representation of the bytes that make up the ArrayBuffer",
	}
}

Compatibility Notes

Currently, Internet Explorer and Safari browsers do not support BigIntArrays.

Credits

The code for these utility functions was inspired by a gist by Jimmy Wärting, but rebuilt from scratch in order to be:

  • human readable,
  • clear in purpose,
  • properly documented and
  • well tested.

Keywords

FAQs

Package last updated on 30 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc